package cn.com.dev;

import cn.com.kh.FDDUtils;
import cn.com.kh.HttpClientProxy;
import cn.com.kh.KHUtil;
import net.sf.json.JSONObject;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

/**
 * @Title: cn.com.dev-DevTao
 * @Description:ngsapitask-ph 系统API接口开发Demo，重点关注业务逻辑部分
 * @Author: lijun
 * @Date 2021-12-03 10:23
 */
public class Dev_FDDCallBack {
    private static final Log log = LogFactory.getLog(Dev_FDDCallBack.class);
    // private static String CC_APIDomain = "http://127.0.0.1:8080/ngsapitask/api";
    private static String CC_APIDomain = "https://ronglian.dbc61.com/ngsapitask_ph/api";
    private static String APPID = "100002";
    private static String APPKEY = "E4uX9c65e5h7A5K5DA6rw6JL";

    public static void main(String[] args) throws Exception {

        String call_Type = "1";
        String rec_Guid = "8993D01E-FB5D-CA85-4E93-DE9F7CDFF4E4";

        if (call_Type.equals("1")) {
            // 模拟认证回调
            SendCert_Callback(rec_Guid, "00810643", "审核通过");
        } else if (call_Type.equals("2")) {
            // 模拟乙方签署回调
            SignB_Callback(rec_Guid, "67d95cd3549f11ef9d88002b6719ffc8", "操作成功");
        } else if (call_Type.equals("3")) {
            // 模拟甲方签署回调
            SignA_Callback(rec_Guid, "67d9ffa7549f11ef9d88002b6719ffc8", "操作成功");
        } else if (call_Type.equals("4")) {
            // 模拟甲方签署回调
            SignA_Callback(rec_Guid, "67d9fffa549f11ef9d88002b6719ffc8", "操作成功");
        }

    }

    // 模拟认证回调
    public static void SendCert_Callback(String recGuid, String customerId, String statusDesc) {

        try {

            JSONObject jsonPara = new JSONObject();
            jsonPara.put("customerId", customerId);
            jsonPara.put("statusDesc", statusDesc);

            if (statusDesc.equals("审核通过")) {
                jsonPara.put("status", "2");
                jsonPara.put("auditFailReason", "");
            } else {
                jsonPara.put("status", "1");
                jsonPara.put("auditFailReason", "模拟回调认证失败");

            }

            JSONObject jsons = FDDUtils.getCallBody(APPID, APPKEY, jsonPara.toString());
            log.debug("模拟认证回调 参数:" + jsons.toString());

            String callResStr = HttpClientProxy.sendPost(CC_APIDomain + "/apiBatchSignMain?callbackType=" + KHUtil.encode("个人认证") + "&customerId=" + customerId + "&recGuid=" + encode(recGuid), jsons.toString(), "", 0);
            log.debug("模拟认证回调 结果:" + callResStr);

        } catch (Exception e) {
            log.error("模拟认证回调 异常", e);

        }

    }

    // 模拟乙方签署回调
    public static void SignB_Callback(String rec_Guid, String transactionNo, String statusDesc) {

        try {

            JSONObject jsonPara = new JSONObject();

            jsonPara.put("transactionNo", transactionNo);
            jsonPara.put("resultMsg", statusDesc);
            if (statusDesc.equals("操作成功")) {
                jsonPara.put("resultCode", 1);
                jsonPara.put("docNo", "模拟乙方签署回调成功");
            } else {
                jsonPara.put("resultCode", 2);
                jsonPara.put("docNo", "模拟乙方签署回调失败");

            }

            JSONObject jsons = FDDUtils.getCallBody(APPID, APPKEY, jsonPara.toString());
            log.debug("模拟乙方签署回调 参数:" + jsons.toString());

            String callResStr = HttpClientProxy.sendPost(CC_APIDomain + "/apiBatchSignMain?callbackType=" + KHUtil.encode("乙方签署回调") + "&customerType=" + KHUtil.encode("个人") + "&recGuid=" + rec_Guid + "&agreementDuid=" + transactionNo, jsons.toString(), "", 0);
            log.debug("模拟乙方签署回调 结果:" + callResStr);

        } catch (Exception e) {
            log.error("模拟乙方签署回调 异常", e);

        }

    }

    // 模拟甲方签署回调
    public static void SignA_Callback(String rec_Guid, String transactionNo, String statusDesc) {

        try {

            JSONObject jsonPara = new JSONObject();

            jsonPara.put("transactionNo", transactionNo);
            jsonPara.put("resultMsg", statusDesc);
            if (statusDesc.equals("操作成功")) {
                jsonPara.put("resultCode", 1);
                jsonPara.put("docNo", "模拟甲方签署回调成功");
            } else {
                jsonPara.put("resultCode", 2);
                jsonPara.put("docNo", "模拟甲方签署回调失败");

            }

            JSONObject jsons = FDDUtils.getCallBody(APPID, APPKEY, jsonPara.toString());
            log.debug("模拟甲方签署回调 参数:" + jsons.toString());

            String callResStr = HttpClientProxy.sendPost(CC_APIDomain + "/apiBatchSignMain?callbackType=" + KHUtil.encode("甲方签署回调") + "&customerType=" + KHUtil.encode("公司") + "&recGuid=" + rec_Guid + "&agreementDuid=" + transactionNo, jsons.toString(), "", 0);
            log.debug("模拟甲方签署回调 结果:" + callResStr);

        } catch (Exception e) {
            log.error("模拟甲方签署回调 异常", e);

        }

    }

    // 工具方法
    public static String encode(String input) {
        if (input == null) {
            return "";
        }

        try {
            return URLEncoder.encode(input, "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        return input;
    }
}